<template>
  <page-header-wrapper>
    <global-search
      :list="searchList"
      @search="handleSearch(false, $event)"
      @reset="handleSearch(true, $event)"
    />
    <a-card :bordered="false">
      <s-table
        ref="table"
        :rowKey="record => record.UserID"
        :columns="columns"
        :data="loadData"
        :scroll="{x: true}"
        :pagination="{ showTotal: total => `共 ${total} 条` }"
      >
        <!-- 表格操作栏 -->
        <template slot="tabAction">
          <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增用户</a-button>
        </template>
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text, record">
          <a-switch :key="text" :default-checked="text === 0 ? true : false" checked-children="启用" un-checked-children="停用" @change="checked=> handleChangeSwitch(checked, record)" />
        </span>
        <span slot="department" slot-scope="text">
          <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="date" slot-scope="text">{{ text | moment }}</span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a v-action:update @click="handleEdit(record)">修改<a-divider type="vertical" /></a>
            <a-popconfirm
              title="您确定是否重置当前用户密码?"
              ok-text="是"
              cancel-text="否"
              placement="bottomLeft"
              @confirm="handleResetPassword(record)"
            >
              <a-icon slot="icon" type="question-circle-o" />
              <a href="#">重置密码</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>
    <edit-user v-if="visible" :visibles.sync="visible" :propsData="propsData"></edit-user>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getUserList, resetPassword } from '@/api/system'
import EditUser from './edit'
const columns = [
  {
    title: '序号',
    align: 'center',
    width: 80,
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '用户名称',
    align: 'center',
    dataIndex: 'UserName',
    width: 160
  },
  {
    title: '登录账号',
    align: 'center',
    dataIndex: 'LoginName'
  },
  {
    title: '联系电话',
    align: 'center',
    dataIndex: 'Phone'
  },
  {
    title: '所属部门',
    align: 'center',
    dataIndex: 'departmentName',
    scopedSlots: { customRender: 'department' }
  },
  {
    title: '是否启用',
    align: 'center',
    dataIndex: 'Mode',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    align: 'center',
    fixed: 'right',
    dataIndex: 'action',
    width: 150,
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'UserList',
  components: {
    STable,
    Ellipsis,
    EditUser
  },
  data () {
    this.columns = columns
    return {
      visible: false,
      // 查询参数
      queryParam: {
        LoginName: '',
        userid: this.$store.getters.userid
      },
      propsData: {
        type: 'add',
        id: ''
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getUserList(requestParameters).then(res => {
          return res
        })
      }
    }
  },
  computed: {
    searchList: function () {
      return [
        {
          label: '登录账号',
          fields: 'LoginName',
          type: 'input',
          props: {
            placeholder: '请输入登录账号'
          }
        }
      ]
    }
  },
  methods: {
    handleAdd () {
      this.visible = true
      this.propsData.type = 'add'
    },
    handleEdit (record) {
      this.visible = true
      this.propsData = {
        type: 'update',
        id: record.UserID
      }
    },
    handleSearch (refresh, strContent) {
      this.queryParam = strContent
      this.$refs.table.refresh(refresh)
    },
    handleChangeSwitch (checked, row) {
      // this.$message.loading(`正在${checked ? '启用' : '停用'}账号中...`)
      // .then(() => {
      //   setUsers({
      //     State: checked ? 'enable' : 'disenable',
      //     sys_users_Modules: { UserID: row.UserID }
      //   }).then((res) => {
      //     if (res.code === 200) {
      //       this.$message.success(`${checked ? '启用' : '停用'}成功`)
      //       this.$refs.table.refresh()
      //     } else {
      //       this.$message.error(`${checked ? '启用' : '停用'}失败, 请稍后重试`)
      //     }
      //   }).catch((err) => {
      //     this.$message.error(`${err.response.statusText || '请检查网络状态或稍后重试'}， 状态码: ${err.response.status || 0}`)
      //   })
      // })
    },
    handleResetPassword (record) {
      this.$message.loading(`正在为${record.UserName}重置密码中...`, 1)
      .then(() => {
        resetPassword({ userID: record.UserID }).then(res => {
          if (res.code === 200) {
            this.$message.success('用户密码重置成功')
          } else {
            this.$message.error('用户密码重置失败, 请稍后重试')
          }
        }).catch((err) => {
          this.$message.error(`${err.response.statusText || '请检查网络状态或稍后重试'}， 状态码: ${err.response.status || 0}`)
        })
      })
    }
  }
}
</script>
